package com.bf.zk.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.bf.zk.constants.Constants;
import com.bf.zk.user.User;
import com.bf.tool.ConfirmInfo;

public class UserAdd extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JDialog userAdd;
	private JTextField userName;
	private JTextField userPwd;
	@SuppressWarnings("unused")
	private JFrame parent;

	public UserAdd(JFrame parent, boolean modal) {
		this.parent = parent;
		JDialog.setDefaultLookAndFeelDecorated(true);
		userAdd = new JDialog(parent, "用户增加", true);
		userAdd.setSize(400, 240);
		userAdd.setDefaultCloseOperation(2);
		// noteManager.setLocation(200, 200);
		// 居中显示
		userAdd.setLayout(null);
		userAdd.setLocationRelativeTo(parent);

		// 名称
		JLabel label1 = new JLabel("用户名称：");
		label1.setBounds(50, 10, 80, 30);
		userName = new JTextField("");
		userName.setBounds(130, 10, 200, 30);

		// 密码
		JLabel label2 = new JLabel("密  码：");
		label2.setBounds(50, 70, 80, 30);
		userPwd = new JTextField("");
		userPwd.setBounds(130, 70, 200, 30);

		// 用户增加确定
		JButton userAdd1 = new JButton("确定");
		userAdd1.setActionCommand("userAdd1");
		userAdd1.setBounds(50, 140, 130, 40);

		// 用户增加取消
		JButton userCancel = new JButton("取消");
		userCancel.setActionCommand("userCancel");
		userCancel.setBounds(200, 140, 130, 40);

		userAdd.add(label1);
		userAdd.add(label2);
		userAdd.add(userName);
		userAdd.add(userPwd);
		userAdd.add(userAdd1);
		userAdd.add(userCancel);

		userAdd1.addActionListener(this);
		userCancel.addActionListener(this);
		userAdd.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 用户增加确定
		if (e.getActionCommand().equals("userAdd1")) {
			if ((ConfirmInfo.ConfirmUser(userName.getText()))&&(ConfirmInfo.ConfirmPwd(userPwd.getText()))&&(!(userName.getText().equals("admin")))) {
				String fileName = "./sqlite/znzk.db";
				try {
					Class.forName("org.sqlite.JDBC");
					Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
					Statement stmt = conn.createStatement();
					boolean rs = stmt.execute("INSERT INTO znzk_user (username,password) VALUES ('"+ userName.getText() + "','"+ userPwd.getText() + "')");
					if (rs) {
						JOptionPane.showMessageDialog(userAdd, "添加失败!",	"警告", JOptionPane.WARNING_MESSAGE);
					}else {
						ResultSet rs0 = stmt
								.executeQuery("SELECT userid FROM znzk_user where username='" + userName.getText() + "';");
						Constants.addUser=new User();
						Constants.addUser.setChooseUser(false);
						Constants.addUser.setUserId(rs0.getString("userid"));
						Constants.addUser.setUserName(userName.getText());
						Constants.addUser.setUserPwd(userPwd.getText());
						Constants.userOrNot=true;
						userAdd.setVisible(false);
					}
					conn.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			} else if(!(ConfirmInfo.ConfirmUser(userName.getText()))&&(ConfirmInfo.ConfirmPwd(userPwd.getText()))){
				JOptionPane.showMessageDialog(userAdd, "用户名不符合规则(数字、字母、下划线组成)!","警告", JOptionPane.WARNING_MESSAGE);
				//userName.setText("");
			}else if((ConfirmInfo.ConfirmUser(userName.getText()))&&!(ConfirmInfo.ConfirmPwd(userPwd.getText()))) {
				JOptionPane.showMessageDialog(userAdd, "密码不符合规则(数字、字母、下划线组成，长度6至10位)!",
						"警告", JOptionPane.WARNING_MESSAGE);
				//userPwd.setText("");
			}else {
				JOptionPane.showMessageDialog(userAdd, "用户名、密码不符合规则(数字、字母、下划线组成，密码长度6至10位)!",
						"警告", JOptionPane.WARNING_MESSAGE);
				//userName.setText("");
				//userPwd.setText("");
			}
		}

		// 用户增加取消
		if (e.getActionCommand().equals("userCancel")) {
			Constants.userOrNotCan=true;
			userAdd.setVisible(false);
		}
	}
}
